package com.miplus.ccrm.workflow.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.EnumRCode;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.ServiceException;
import com.miplus.ccrm.workflow.model.common.DeleteRequest;
import com.miplus.ccrm.workflow.model.dto.knowledge.KnowledgeBaseCreateDTO;
import com.miplus.ccrm.workflow.model.dto.knowledge.KnowledgeBaseQueryDTO;
import com.miplus.ccrm.workflow.model.dto.knowledge.KnowledgeBaseUpdateDTO;
import com.miplus.ccrm.workflow.model.po.WorkflowKnowledgeBase;
import com.miplus.ccrm.workflow.model.vo.KnowledgeBaseVO;
import com.miplus.ccrm.workflow.service.WorkflowKnowledgeBaseService;
import com.miplus.ccrm.workflow.mapper.WorkflowKnowledgeBaseMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author feixiang
 * @description 针对表【workflow_knowledge_base(知识库表)】的数据库操作Service实现
 * @createDate 2025-11-04 20:48:14
 */
@Service
public class WorkflowKnowledgeBaseServiceImpl extends MPJBaseServiceImpl<WorkflowKnowledgeBaseMapper, WorkflowKnowledgeBase>
        implements WorkflowKnowledgeBaseService {

    @Override
    public CmdResVo cereateKnowledgeBase(KnowledgeBaseCreateDTO knowledgeBaseDTO) {
        if (knowledgeBaseDTO == null) {
            throw new ServiceException(EnumRCode.ArgErr.value, EnumRCode.ArgErr.name);
        }
        WorkflowKnowledgeBase workflowKnowledgeBase = new WorkflowKnowledgeBase();
        BeanUtils.copyProperties(knowledgeBaseDTO, workflowKnowledgeBase);
        this.save(workflowKnowledgeBase);
        return new CmdResVo(1, workflowKnowledgeBase.getPid().toString());
    }

    @Override
    public CmdResVo updateKnowledgeBase(KnowledgeBaseUpdateDTO knowledgeBaseUpdateDTO) {
        if (knowledgeBaseUpdateDTO == null) {
            throw new ServiceException(EnumRCode.ArgErr.value, EnumRCode.ArgErr.name);
        }
        //判断是否存在
        WorkflowKnowledgeBase workflowKnowledgeBase = this.getById(knowledgeBaseUpdateDTO.getPid());
        if (workflowKnowledgeBase == null) {
            throw new ServiceException(EnumRCode.DataNotExist.value, EnumRCode.DataNotExist.name);
        }
        BeanUtils.copyProperties(knowledgeBaseUpdateDTO, workflowKnowledgeBase);
        this.updateById(workflowKnowledgeBase);
        return new CmdResVo(1, workflowKnowledgeBase.getPid().toString());
    }

    @Override
    public CmdResVo deleteById(Long id) {
        if (id == null) {
            throw new ServiceException(EnumRCode.ArgErr.value, EnumRCode.ArgErr.name);
        }
        //判断是否存在
        WorkflowKnowledgeBase workflowKnowledgeBase = this.getById(id);
        if (workflowKnowledgeBase == null) {
            throw new ServiceException(EnumRCode.DataNotExist.value, EnumRCode.DataNotExist.name);
        }
        this.removeById(id);
        return new CmdResVo(1, id.toString());
    }

    @Override
    public IPage<KnowledgeBaseVO> getKnowledgeBaseList(KnowledgeBaseQueryDTO query) {
        if (query == null) {
            throw new ServiceException(EnumRCode.ArgErr.value, EnumRCode.ArgErr.name);
        }
        return selectJoinListPage(new Page<>(query.getPageNum(), query.getPageSize()),
                KnowledgeBaseVO.class,
                new MPJLambdaWrapper<WorkflowKnowledgeBase>()
                        .selectAll(WorkflowKnowledgeBase.class)
                        .eq(JPubUtils.IsNotEmpty(query.getName()), WorkflowKnowledgeBase::getName, query.getName())
                        .eq(JPubUtils.IsNotEmpty(query.getStatus()), WorkflowKnowledgeBase::getStatus, query.getStatus())
                        .between(JPubUtils.IsNotEmpty(query.getStartTime()), WorkflowKnowledgeBase::getCreatedTime, query.getStartTime(), query.getEndTime())
                        .orderByDesc(WorkflowKnowledgeBase::getCreatedTime)
        );
    }

    @Override
    public CmdResVo deleteKnowledgeBase(DeleteRequest deleteRequest) {
        if (deleteRequest == null) {
            throw new ServiceException(EnumRCode.ArgFormatErr.value, EnumRCode.ArgFormatErr.name);
        }
        //判断是否存在
        WorkflowKnowledgeBase workflowKnowledgeBase = this.getById(deleteRequest.getId());
        if (workflowKnowledgeBase == null) {
            throw new ServiceException(EnumRCode.DataNotExist.value, EnumRCode.DataNotExist.name);
        }
        this.removeById(deleteRequest.getId());
        return new CmdResVo(1, deleteRequest.getId().toString());
    }

    @Override
    public List<KnowledgeBaseVO> getEnableKnowledgeBaseList() {
        return selectJoinList(KnowledgeBaseVO.class,
                new MPJLambdaWrapper<WorkflowKnowledgeBase>()
                        .selectAll(WorkflowKnowledgeBase.class)
                        .eq(WorkflowKnowledgeBase::getStatus, 1)
        );
    }


}